﻿using NotificationLib;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataSetNotificaiton
{
    class Program
    {

        private static DataTable GetDataTable(SqlCommand cmd)
        {
            if(cmd.Connection.State == ConnectionState.Closed || cmd.Connection.State == ConnectionState.Broken)
                cmd.Connection.Open();


            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            return dt;

        }
        
        static string GetConfigValue(string name, List<DataRow> list)
        {
            return list.First(i => i["ConfigKey"].ToString() == name)["ConfigValue"].ToString();
        }

        static void Main(string[] args)
        {

            string path = System.Reflection.Assembly.GetEntryAssembly().CodeBase;

            string dir = Path.GetDirectoryName(path);

            Uri u = new Uri(dir);

            Directory.SetCurrentDirectory(u.LocalPath);

            string connectionString = File.ReadAllText("ConnectionString.txt");

             using (SqlConnection conn = new SqlConnection(connectionString))
             {
                 conn.Open();
                 SqlCommand cmd = new SqlCommand();
                 cmd.Connection = conn;
                 cmd.CommandType = System.Data.CommandType.StoredProcedure;
                 cmd.CommandText = "usp_GetCompletedDataRequestsPendingNotification";

                 DataTable dt = GetDataTable(cmd);

                 if (dt.Rows.Count > 0)
                 {

                     SqlCommand configCmd = new SqlCommand();
                     configCmd.Connection = conn;
                     configCmd.CommandType = CommandType.Text;
                     configCmd.CommandText = "SELECT * FROM CONFIG;";

                     DataTable configDt = GetDataTable(configCmd);

                     List<DataRow> list = configDt.AsEnumerable().ToList();

                     

                     foreach (DataRow r in dt.Rows)
                     {
                         if ((bool)r["Pushover"])
                         {

                             PushoverNotificationConfig pnc = new PushoverNotificationConfig();
                             pnc.ApiKey = GetConfigValue("PushoverApiKey", list);

                             PushoverNotification n = new PushoverNotification(pnc, r["PushoverKey"].ToString(), "Galactic", r["Title"].ToString());

                             n.Send();
                         }

                         if( (bool)r["Email"])
                         {
                             EmailNotificationConfig enc = new EmailNotificationConfig();
                             enc.AuthenticationType = EmailNotificationAuthenticationType.UserAndPass;
                             enc.MailServer = GetConfigValue("MailServer", list);
                             enc.MailServerPort = short.Parse(GetConfigValue("MailPort", list));
                             enc.FromAddress = new System.Net.Mail.MailAddress(GetConfigValue( "MailFrom", list), GetConfigValue( "MailFromName", list));
                             enc.Username = GetConfigValue( "MailUser", list);
                             enc.Password = GetConfigValue( "MailPassword", list);
                             string address = r["EmailAddress"].ToString();
                             
                             EmailNotification notif = new EmailNotification(enc, "Galactic", r["Title"].ToString(), address);

                             notif.Send();
                         }                         
                     }
                 }
                 

             }
        }
    }
}
